package nl.rainbowsheep.reisorg.cust.site.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.cust.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.cust.site.model.Country;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class CountryService {

    @Value("${api.url}")
    String url;

    public List<Country> getAll() {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        List<Country> countries = template.getForObject(url + "/country/getall?" + getLocaleParam(), List.class);
        return countries;
    }

    /**
     * Get current locale (from cookie)
     *
     * @return string with locale param
     */
    private String getLocaleParam() {
        return "locale=" + LocaleContextHolder.getLocale().getLanguage();
    }

}
